import { _decorator, Component, dragonBones, Input, Label, Node, RealInterpolationMode, Sprite, Vec3 } from 'cc';
import { GAME_SPRITEFRAME, GAME_TOP, GlobalData, SOUND_TYPE } from '../../../Data/GlobalData';
import { DragonLoad } from '../DragonLoad/DragonLoad';
import { DragonBody } from '../DragonLoad/DragonBody';
import { TweenTools } from '../../../Tool/TweenTools';
import { Tools } from '../../../Tool/Tools';
import { ButtleLoad } from '../ButtleLoad/ButtleLoad';
import { TopPromptText } from '../../../Top/TopPromptText';
const { ccclass, property } = _decorator;

@ccclass('SingleGun')
/**
 * 单个炮台
 */
export class SingleGun extends Component {

    //炮台样式
    @property(Node)
    private Cat: Node = null;

    //炮台数量文字
    @property(Label)
    private CountLable: Label = null;

    //龙层
    @property(DragonLoad)
    private DragonLoad: DragonLoad = null;

    //炮台索引
    public GunIndex: number = 0;

    //炮台数量
    public GunCount: number = 0;

    //炮台层
    @property(Node)
    public GunLoad: Node = null;
    //大炮
    @property(Node)
    private Gun: Node = null;

    //爆照效果
    @property(Node)
    private Boom: Node = null;

    //子弹层
    @property(Node)
    private ButtleLoad: Node = null;

    //猫咪层
    @property(Node)
    private CatLoad: Node = null;

    //锁层
    @property(Node)
    public LockNode: Node = null;

    protected start(): void {
        this.node.on(Input.EventType.TOUCH_START, this.UnLock, this);
    }

    /**
     * 初始化
     */
    public Init() {
        this.GunLoad.active = false;
        this.Gun.active = false;
        this.Boom.active = false;
        this.LockNode.active = false;
    }

    /**
     * 我是需要看广告解锁得
     */
    public IamLock() {
        this.LockNode.active = true;
    }

    /**
     * 解锁
     */
    private UnLock() {
        if (!this.LockNode.active) return;

        GlobalData.GameOver = true;
        app.adManager.ShowVideo(0, (_isend: boolean) => {
            GlobalData.GameOver = false;
            if (_isend) {
                this.LockNode.active = false;
                app.topManager.ShowPrompt("成功解锁新炮台");
            } else {
                app.topManager.ShowPrompt("解锁失败");
            }
        })


    }

    /**
     * 更新炮台样式
     */
    public UpdateGunStyle(_gunindex: number, _guncount: number) {
        this.GunLoad.active = true;
        this.Gun.active = true;
        this.Gun.angle = 0;
        //添加一个播放动画
        this.GunIndex = _gunindex;
        this.GunCount = _guncount;
        this.CountLable.string = "X" + _guncount.toString();
        this.Cat.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames[GAME_SPRITEFRAME.Gun + this.GunIndex]

        TweenTools.ScaleBigNoSmall(this.CatLoad, 0.1);
        this.scheduleOnce(this.Fire.bind(this), app.gameData.FireTime);
    }

    /**
     * 播放发射动画
     */
    private PlayFireAni(_bodyworldpos: Vec3) {
        app.audioManager.PlayEffect(SOUND_TYPE.Fire);
        //发射
        this.GunCount--;
        this.CountLable.string = "X" + this.GunCount.toString();
        //
        this.Gun.angle = Tools.ReturnAngle(this.node.getWorldPosition(), _bodyworldpos) - 90;
        this.Boom.active = true;
        this.scheduleOnce(() => {
            this.Boom.active = false;
        }, 0.2)
        //播放个动画
        TweenTools.Fire1(this.Gun);
        TweenTools.Fire(this.GunLoad)
    }

    /**
     * 发射炮台
     */
    public Fire() {
        if (!this.GunLoad.active) return;

        let is_back: boolean = false;

        //判断回退的是否有可以击中的
        for (let body of this.DragonLoad.BackDragonBodyArr) {
            if (body.getComponent(DragonBody).BodyIndex == this.GunIndex &&
                body.getComponent(DragonBody).ReturnCanHit()) {
                body.getComponent(DragonBody).IsHit = false;
                is_back = true;
                this.PlayFireAni(body.getWorldPosition());

                this.ButtleLoad.getComponent(ButtleLoad).CreateButtle(
                    this.node.getWorldPosition(),
                    this.Gun.angle,
                    this.GunIndex,
                    body
                )
                break;
            }
        }

        if (!is_back) {
            //判断是否有可以击中的
            for (let body of this.DragonLoad.DragonBodyArr) {
                if (body.getComponent(DragonBody).BodyIndex == this.GunIndex &&
                    body.getComponent(DragonBody).ReturnCanHit()) {

                    body.getComponent(DragonBody).IsHit = false;
                    this.PlayFireAni(body.getWorldPosition());

                    this.ButtleLoad.getComponent(ButtleLoad).CreateButtle(
                        this.node.getWorldPosition(),
                        this.Gun.angle,
                        this.GunIndex,
                        body
                    )
                    break;
                }
            }
        }

        //发射
        if (this.GunCount > 0) {
            this.scheduleOnce(this.Fire.bind(this), app.gameData.FireTime - app.gameData.GameData.gunlevel * 0.1);
        } else if (this.GunCount <= 0) {
            this.scheduleOnce(this.Init.bind(this), app.gameData.FireTime - app.gameData.GameData.gunlevel * 0.1);
        }

    }

}


